package com.pope.framework.ambulance.modular.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pope.framework.ambulance.modular.dto.AmbulanceInfoDto;
import com.pope.framework.ambulance.modular.dto.AmbulanceInfoRegisterDto;
import com.pope.framework.ambulance.modular.entity.AmbulanceInfo;
import com.pope.framework.ambulance.modular.service.AmbulanceInfoService;
import com.pope.framework.ambulance.modular.vo.AmbulanceInfoVo;
import com.pope.framework.constants.AmbulanceComingState;
import com.pope.framework.constants.AmbulanceRegisterState;
import com.pope.framework.core.reqres.response.ResponseData;
import com.pope.framework.dict.ambulance.modular.entity.DictComingState;
import com.pope.framework.dict.ambulance.modular.entity.DictRegisterState;
import com.pope.framework.dict.ambulance.modular.service.DictComingStateService;
import com.pope.framework.dict.ambulance.modular.service.DictRegisterStateService;
import com.pope.framework.kernel.model.exception.ServiceException;
import com.pope.framework.kernel.model.page.PageResult;
import com.pope.framework.page.PageFactory;
import com.pope.framework.triage.modular.entity.PatientInfo;
import com.pope.framework.triage.modular.vo.PatientInfoVo;
import com.pope.framework.triage.modular.vo.TriageSimpleInfoVo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@RestController
//@CrossOrigin(allowCredentials="true",maxAge = 3600)
@CrossOrigin
@RequestMapping("${spring.application.app-id}/ambulance")
@Api(value="救护车登记相关API",tags= {"救护车登记相关APIAPI"})
public class AmbulanceInfoController {
	@Autowired
	private AmbulanceInfoService ambulanceInfoService;
	
	@Autowired
	private DictComingStateService dictComingStateService;
	@Autowired
	private DictRegisterStateService dictRegisterStateService;
	@PostMapping("cominging/register")
	@ApiOperation(value = "救护车来院路上信息登记",notes = "救护车来院路上信息登记")
	public ResponseData<Integer> select(@RequestBody @Valid AmbulanceInfoDto mbulanceInfoDto,BindingResult bindingResult){
		
		AmbulanceInfo queryAmbulanceInfo=new AmbulanceInfo();
		queryAmbulanceInfo.setComingState(AmbulanceComingState.WDY.getCode());
		queryAmbulanceInfo.setEmergencyRegisterState(AmbulanceRegisterState.WDJ.getCode());
		queryAmbulanceInfo.setHospitalId(mbulanceInfoDto.getHospitalId());
		queryAmbulanceInfo.setPlateNumber(mbulanceInfoDto.getPlateNumber());
		QueryWrapper<AmbulanceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.setEntity(queryAmbulanceInfo);
		queryWrapper.orderByDesc("UPDATETIME");
		AmbulanceInfo ambulanceInfo=ambulanceInfoService.getOne(queryWrapper);
		
		if(mbulanceInfoDto.getComingState().equals(AmbulanceComingState.WDY.getCode())) {
			if(ambulanceInfo==null) {
				ambulanceInfo=new AmbulanceInfo();
				BeanUtil.copyProperties(mbulanceInfoDto, ambulanceInfo);
				ambulanceInfo.setEmergencyRegisterState(AmbulanceRegisterState.WDJ.getCode());
				//ambulanceInfo.setCreatetime(DateUtil.now());
				//ambulanceInfo.setUpdatetime(DateUtil.now());
				ambulanceInfoService.save(ambulanceInfo);
			}else {
				BeanUtil.copyProperties(mbulanceInfoDto, ambulanceInfo);
				
				//ambulanceInfo.setUpdatetime(DateUtil.now());
				ambulanceInfoService.updateById(ambulanceInfo);
			}
		}else {
			if(ambulanceInfo==null) {
				throw new ServiceException("当前不存在未到院救护车，请重新确认！");
			}else {
				BeanUtil.copyProperties(mbulanceInfoDto, ambulanceInfo);
				
				ambulanceInfoService.updateById(ambulanceInfo);
			}
		}
		
		return ResponseData.success(ambulanceInfo.getId());
	} 
	
	
	@PostMapping("emergency/register")
	@ApiOperation(value = "救护车到院后急诊信息登记",notes = "救护车到院后急诊信息登记")
	public ResponseData<Integer> select(@RequestBody @Valid AmbulanceInfoRegisterDto mbulanceInfoRegisterDto,BindingResult bindingResult){
		
		AmbulanceInfo aAmbulanceInfo=new AmbulanceInfo();
		aAmbulanceInfo.setId(mbulanceInfoRegisterDto.getId());
		aAmbulanceInfo.setComingState(AmbulanceComingState.YDY.getCode());
		aAmbulanceInfo.setEmergencyRegisterState(AmbulanceRegisterState.YDJ.getCode());
		aAmbulanceInfo.setTriageId(mbulanceInfoRegisterDto.getTriageId());
		boolean falg=ambulanceInfoService.updateById(aAmbulanceInfo);
		if(falg) {
			return ResponseData.success(aAmbulanceInfo.getId()); 
		}else {
		  throw new ServiceException("不存在该救护车信息！");	
		} 
	} 
	
	@GetMapping("list")
	@ApiOperation(value = "查询当前救护车信息",notes = "查询当前救护车信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comingstate",value = "到院状态,1未到院2已到院",dataType = "String",paramType = "query",example = "1",required=false),
            @ApiImplicitParam(name = "emergencyRegisterState",value = "急诊登记状态1未登记2已经登记",dataType = "String",paramType = "query",example = "1",required=false),
            @ApiImplicitParam(name = "hospitalId",value = "所在医院ID",dataType = "Integer",paramType = "query",example = "1",required=false),
            @ApiImplicitParam(name = "pagestart",value = "第几页，从1开始，默认为1",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pagenum",value = "每页显示记录数，默认为20",dataType = "String",paramType = "query",example = "10"),
            @ApiImplicitParam(name = "orderby",value = "排序字段，默认为主键ID，可排序字段有coming_Time(几分钟到院)/COMING_STATE(到院状态)/EMERGENCY_REGISTER_STATE(急诊登记状态)",dataType = "String",paramType = "query",example = "id"),
            @ApiImplicitParam(name = "sort",value = "排序方式，有asc,desc,默认为asc",dataType = "String",paramType = "query",example = "desc"),
            
    })
	public ResponseData<PageResult<AmbulanceInfoVo>> list(String comingstate,String emergencyRegisterState, Integer hospitalId){
		List<DictComingState> dictComingStates=dictComingStateService.list();
		List<DictRegisterState> dictRegisterStates=dictRegisterStateService.list();
		Page<AmbulanceInfo> page=PageFactory.defaultPage();
		AmbulanceInfo queryAmbulanceInfo=new AmbulanceInfo();
		if(StrUtil.isNotEmpty(comingstate)) {
			queryAmbulanceInfo.setComingState(comingstate);
		}
		if(StrUtil.isNotEmpty(emergencyRegisterState)) {
			queryAmbulanceInfo.setEmergencyRegisterState(emergencyRegisterState);
		}
		if(hospitalId!=null) {
			queryAmbulanceInfo.setHospitalId(hospitalId);
		}
		QueryWrapper<AmbulanceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.setEntity(queryAmbulanceInfo);
		IPage<AmbulanceInfo> data=ambulanceInfoService.page(page, queryWrapper);
		List<AmbulanceInfoVo> ambulanceInfoVos=CollUtil.newArrayList();
		if(CollUtil.isNotEmpty(data.getRecords())) {
			for(AmbulanceInfo ambulanceInfo:data.getRecords()) {
				AmbulanceInfoVo aAmbulanceInfoVo=new AmbulanceInfoVo();
				BeanUtil.copyProperties(ambulanceInfo, aAmbulanceInfoVo);
				ambulanceInfoVos.add(aAmbulanceInfoVo);
				for(DictComingState dictComingState:dictComingStates) {
					if(dictComingState.getCode().equals(ambulanceInfo.getComingState())) {
						aAmbulanceInfoVo.setComingStateName(dictComingState.getName());
						break;
					}
				}
				for(DictRegisterState dictRegisterState:dictRegisterStates) {
					if(dictRegisterState.getCode().equals(ambulanceInfo.getEmergencyRegisterState())) {
						aAmbulanceInfoVo.setEmergencyRegisterStateName(dictRegisterState.getName());
						break;
					}
				}
			}
		}
		IPage<AmbulanceInfoVo> newPage=new Page<AmbulanceInfoVo>();
		newPage.setCurrent(data.getCurrent());
		newPage.setPages(data.getPages());
		newPage.setSize(data.getSize());
		newPage.setTotal(data.getTotal());
		newPage.setRecords(ambulanceInfoVos);
		return ResponseData.success(PageFactory.pageResult(newPage));
	}  
	
}
